package com.stevencheong.matrixSolver;

import Jama.Matrix;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MatrixSolver extends Activity {
	
	private EditText a00;
	private EditText a10;
	private EditText a20;
	private EditText a01;
	private EditText a11;
	private EditText a21;
	private EditText a02;
	private EditText a12;
	private EditText a22;
	
	private EditText b00;
	private EditText b10;
	private EditText b20;
	private EditText b01;
	private EditText b11;
	private EditText b21;
	private EditText b02;
	private EditText b12;
	private EditText b22;
	
	private EditText c00;
	private EditText c10;
	private EditText c20;
	private EditText c01;
	private EditText c11;
	private EditText c21;
	private EditText c02;
	private EditText c12;
	private EditText c22;
	
	Matrix a_matrix;
	Matrix b_matrix;
	Matrix c_matrix;
	
	private MatrixSolver mContext;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        
        setContentView(R.layout.matrix);
        
        // Initialize EditText's
        a00 = (EditText) findViewById(R.id.a00);
        a10 = (EditText) findViewById(R.id.a10);
        a20 = (EditText) findViewById(R.id.a20);
        a01 = (EditText) findViewById(R.id.a01);
        a11 = (EditText) findViewById(R.id.a11);
        a21 = (EditText) findViewById(R.id.a21);
        a02 = (EditText) findViewById(R.id.a02);
        a12 = (EditText) findViewById(R.id.a12);
        a22 = (EditText) findViewById(R.id.a22);
        
        b00 = (EditText) findViewById(R.id.b00);
        b10 = (EditText) findViewById(R.id.b10);
        b20 = (EditText) findViewById(R.id.b20);
        b01 = (EditText) findViewById(R.id.b01);
        b11 = (EditText) findViewById(R.id.b11);
        b21 = (EditText) findViewById(R.id.b21);
        b02 = (EditText) findViewById(R.id.b02);
        b12 = (EditText) findViewById(R.id.b12);
        b22 = (EditText) findViewById(R.id.b22);
        
        c00 = (EditText) findViewById(R.id.c00);
        c10 = (EditText) findViewById(R.id.c10);
        c20 = (EditText) findViewById(R.id.c20);
        c01 = (EditText) findViewById(R.id.c01);
        c11 = (EditText) findViewById(R.id.c11);
        c21 = (EditText) findViewById(R.id.c21);
        c02 = (EditText) findViewById(R.id.c02);
        c12 = (EditText) findViewById(R.id.c12);
        c22 = (EditText) findViewById(R.id.c22);
        
        // Initialize Buttons
        Button addButton = (Button) findViewById(R.id.btnAdd);
        Button subtractButton = (Button) findViewById(R.id.btnSubtract);
        Button multiplyButton = (Button) findViewById(R.id.btnMultiply);
        
        Button aInv = (Button) findViewById(R.id.btnAinv);
        Button aDet = (Button) findViewById(R.id.btnAdet);
        Button bInv = (Button) findViewById(R.id.btnBinv);
        Button bDet = (Button) findViewById(R.id.btnBdet);
        Button cToA = (Button) findViewById(R.id.btnCtoA);
        Button cToB = (Button) findViewById(R.id.btnCtoB);
        
        // Add Listeners to Matrix Buttons
        addButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoMatrices();
					c_matrix = a_matrix.plus(b_matrix);
					setMatrixCTextFields();
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        subtractButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoMatrices();
					c_matrix = a_matrix.minus(b_matrix);
					setMatrixCTextFields();
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        multiplyButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoMatrices();
					c_matrix = a_matrix.times(b_matrix);
					setMatrixCTextFields();
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        aInv.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putAInputsIntoMatrixA();
					
					c_matrix = a_matrix.inverse();
					setMatrixCTextFields();
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        aDet.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putAInputsIntoMatrixA();
					
					show = new AlertDialog.Builder(mContext).setTitle("Result")
							.setMessage("det(A) = " + new Double(a_matrix.det()).toString())
							.setPositiveButton("OK", null).show(); 
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        bInv.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putBInputsIntoMatrixB();
					
					c_matrix = b_matrix.inverse();
					setMatrixCTextFields();
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        bDet.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putBInputsIntoMatrixB();
					
					show = new AlertDialog.Builder(mContext).setTitle("Result")
							.setMessage("det(B) = " + new Double(b_matrix.det()).toString())
							.setPositiveButton("OK", null).show(); 
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        cToA.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					a00.setText(c00.getText());
					a01.setText(c01.getText());
					a02.setText(c02.getText());
					a10.setText(c10.getText());
					a11.setText(c11.getText());
					a12.setText(c12.getText());
					a20.setText(c20.getText());
					a21.setText(c21.getText());
					a22.setText(c22.getText());
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        cToB.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					b00.setText(c00.getText());
					b01.setText(c01.getText());
					b02.setText(c02.getText());
					b10.setText(c10.getText());
					b11.setText(c11.getText());
					b12.setText(c12.getText());
					b20.setText(c20.getText());
					b21.setText(c21.getText());
					b22.setText(c22.getText());
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
    }
    
    //helper methods
	private void putAInputsIntoMatrixA() {
		double[][] a_arr = {
				{ new Double(a00.getText().toString()), new Double(a10.getText().toString()), new Double(a20.getText().toString()) },
				{ new Double(a01.getText().toString()), new Double(a11.getText().toString()), new Double(a21.getText().toString()) },
				{ new Double(a02.getText().toString()), new Double(a12.getText().toString()), new Double(a22.getText().toString()) },
		};
		
		a_matrix = new Matrix(a_arr);
	}
    
	private void putBInputsIntoMatrixB() {
		double[][] b_arr = {
				{ new Double(b00.getText().toString()), new Double(b10.getText().toString()), new Double(b20.getText().toString()) },
				{ new Double(b01.getText().toString()), new Double(b11.getText().toString()), new Double(b21.getText().toString()) },
				{ new Double(b02.getText().toString()), new Double(b12.getText().toString()), new Double(b22.getText().toString()) },
		};
		
		b_matrix = new Matrix(b_arr);
	}
	
	private void putInputsIntoMatrices() {
		putAInputsIntoMatrixA();
		putBInputsIntoMatrixB();
	}
	
	private void setMatrixCTextFields() {
		c_matrix = c_matrix.transpose();		//do this to make Matrix.get() intuitive
		
		c00.setText( new Double( c_matrix.get(0, 0) ).toString() );
		c10.setText( new Double( c_matrix.get(1, 0) ).toString() );
		c20.setText( new Double( c_matrix.get(2, 0) ).toString() );
		
		c01.setText( new Double( c_matrix.get(0, 1) ).toString() );
		c11.setText( new Double( c_matrix.get(1, 1) ).toString() );
		c21.setText( new Double( c_matrix.get(2, 1) ).toString() );
		
		c02.setText( new Double( c_matrix.get(0, 2) ).toString() );
		c12.setText( new Double( c_matrix.get(1, 2) ).toString() );
		c22.setText( new Double( c_matrix.get(2, 2) ).toString() );
	}
}